// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        isInited:false,

        isBusy:false,

        
    },


    init(config,data = null){
        //let size = cc.director.getVisibleSize();
       // let scaleX = 
        this.initView(config);
        this.initByData(data);

    },
    //初始化view
    initView(config){
        if(this.isInited )
            return ;
        for(let key in config)
        {
            this.initNode(config[key]);   
        }
        this.isInited = true;
    },

    initByData(data){
        if(!data)
            return ;

    },
    //初始化node
    initNode(config){
        if(config.node != null)
            return ;
        let node = cc.find(config.name,this.node);
        if(!node)
        {
            console.error("invalid node name " ,config.name);
            return ;
        }
        config.node = node;
        if(config.eventType)
        {
            config.node.on(config.eventType,this[config.cb],this);
        }
        if(config.scale != null)
        {
            config.node.scale = config.scale;
        }
        else
        {
            config.node.scale = 1;
        }
        //默认是true;
        config.node.active = config.active == null ? true : false;
    },

    //更新图片或者label的显示
    updateInfo(node,info,type = cc.Label){
        if(!node)
        {
            cc.log("invalid node or info ",node);
            return ;
        }
           
        let com = node.getComponent(type);
        if(type == cc.Label)
        {
            com.string  = info != null ? info : "";
        }
        else if(type == cc.Sprite)
        {
            com.spriteFrame = info ;
        }
    },

    onHide(){
        if(this.node.active)
        {
            this.node.active = false;
        }
    },

    onShow(){
        if(!this.node.active)
        {
            this.node.active = true;
        }
    },


    getRankList(){
        let canvas = cc.director.getScene().getChildByName("Canvas");
        let mainScene = canvas.getComponent("mainScene");
        if(mainScene)
        {
            return mainScene.rankList;
        }
        return [];
    },

    getCakeList(){
        let canvas = cc.director.getScene().getChildByName("Canvas");
        let mainScene = canvas.getComponent("mainScene");
        if(mainScene)
        {
            return mainScene.cakeList;
        }
        return [];
    },

    getCakeByName(imgName){
        if(!imgName)
            return null;
        let cakeList = this.getCakeList();
        return cakeList.getSpriteFrame(imgName);
    },


    getGiftByIndex(name = ""){
        let canvas = cc.director.getScene().getChildByName("Canvas");
        let mainScene = canvas.getComponent("mainScene");
        let list = null;
        if(mainScene)
        {
            list =  mainScene.ggkGiftList;
        }
        if(!name)
        {
            let l = "0";
            let m = Math.floor(Math.random()*10) + "";
            let r =  Math.floor(Math.random()*9) + ""
            name = l+m+r;

        }
        cc.log(name);
        return list.getSpriteFrame(name);
    },

    getHeadByIndex(index){
        let canvas = cc.director.getScene().getChildByName("Canvas");
        let mainScene = canvas.getComponent("mainScene");
        let list = null;
        if(!mainScene)
        {
           return null;
        }

        return mainScene.userHeadList[index%4];
    }

});
